//
//  PointManager.cpp
//  dot
//
//  Created by zz on 2019/6/20.
//

#include "PointManager.hpp"

bool PointManager::init(){
    
    if(!Node::init()){
        return false;
    }
    
    this->size = Director::getInstance()->getVisibleSize();
    return true;
}


void PointManager::createPoint(Vec2 pos){
    
    this->maxY=10;
    this->minY=-10;
    this->maxX=10;
    this->minX=-10;
    if(pos.x<=this->pw){
        
        this->minX=0;
    }
    
    if(pos.x+this->pw>=this->size.width){
        
        this->maxX=0;
    }
    
    if(pos.y<=this->pw){
        
        this->minY=0;
    }
    
    if(pos.y+this->pw>=this->size.height){
    
        this->maxY=0;
    }

    
    float x = this->getRandomRange(this->minX,this->maxX);
    float y = this->getRandomRange(this->minY, this->maxY);
    Vec2 dir = Vec2(x,y).getNormalized();
    float l = sqrt(dir.x*dir.x+dir.y*dir.y);  //这个有点dy
    float offset = this->pw/l;
    
    Vec2 newpos = Vec2(dir.x*offset,dir.y*offset)+pos;
    auto p =  GPoint::create(newpos);
    p->setTag(666);
    this->getParent()->addChild(p);
    //do something;
}


void PointManager::setPlayerWidth(float w){
    
    this->pw = w;
    
}


int PointManager::getRandomRange(int s,int e){
    
    srand(int(time(0))+ rand());
    return (CCRANDOM_0_1()*(e - s + 1) + s);
}
